You can use WPF Diagrams in three ways:
- For many diagramming projects, you will simply use the standard diagram model and its classes. This supports arbitrary diagrams, in which users can place shapes as required, and there are no particular semantics or rules associated with the different shapes.
- If a project requires a specific kind of diagram which is included in WPF Diagrams, such as flow diagrams, then you can use the diagram model and classes for that kind of diagram.
- If a project requires a specific kind of diagram which is not included in WPF Diagrams, such as BPMN process diagrams, then you can use the Foundation classes and interfaces to define a semantic model and rules, and visual styles, for this kind of diagram. The Foundation classes are also used for advanced customisation of the built-in diagram types.
This section describes how to work with the standard diagram model.
- For specific types of diagram included in WPF Diagrams, see Flow Diagrams and Star Diagrams.
- For creating your own kinds of diagram and advanced customisation using the Foundation classes, see Diagram Foundation.
The Diagram Class
A standard diagram is represented by the Diagram class. A diagram consists of a number of nodes and connections, represented by the DiagramNode (frequently ShapeNode) and DiagramConnection classes.A Diagram is purely a data (model) object. It does not contain any WPF UI elements, and contains only minimal presentation information such as node positions and sizes. You can build up a diagram programmatically by adding items to its Nodes and Connections collections, or just create an empty (blank) diagram for your users to place items onto.
To display a diagram, use the DiagramSurface control. The DiagramSurface takes care of all presentation elements, optionally using a DiagramFormatter to control appearance and styling.
this.DataContext = new Diagram();
<ms:DiagramSurface Diagram="{Binding}" />
Nodes and Connections
The base Node type does not contain any information about layout or connectivity. This is provided through derived classes such as TitleNode or ShapeNode, and through templates referenced through the diagram formatter.
The base Connection type uses a default layout which the user can modify. Typically you will use derived types of Connection to specify features such as routing, styling, arrows, etc. through templates referenced through the diagram formatter.
For a step by step example of setting up and displaying a standard diagram, see the QuickStart.
Representing Your Business Objects as a Diagram
You can use Diagram as a model in itself, or you can use it as a view model over a business class. In the latter case, you will need to create adapter code to synchronise the model (business object) and view model (diagram model). The best approach in this case is to have the diagram observe the business object and update itself as the business object changes, but not vice versa. WPF Diagramming supports this by allowing you to map user actions (such as creating nodes and connections) to business object actions, thereby avoiding the need for two-way synchronisation.